import psycopg2
import requests

# 连接到 PostgreSQL 数据库
conn = psycopg2.connect(
    dbname="x-easy-digital-twins-prod",
    user="digital_3d",
    password="xtc#2023@3d",
    host="10.11.2.97",
    port="4101",
)


# 获取模型的url数据
def getModelUrlData():
    # 创建一个游标对象，用于执行 SQL 语句
    cur = conn.cursor()

    # 执行 SQL 查询
    cur.execute(f"SELECT * FROM dt_models")

    # 获取查询结果
    rows = cur.fetchall()

    modelsData = []

    # 打印查询结果
    for row in rows:
        modelsData.append(row[4])
        modelsData.append(row[14])

    # 关闭游标对象
    cur.close()

    # 关闭数据库连接
    conn.close()

    return set(modelsData)


# 下载模型数据到本地
def downModel(modelUrl: str):
    url = f"https://10.11.1.130:5082/three-d/{modelUrl}"

    print(url)

    response = requests.get(url, verify=False)

    if response.status_code == 200:
        with open(f"models/{modelUrl}", "wb") as file:
            file.write(response.content)
        print("文件下载成功！")
    else:
        print("文件下载失败:", response.status_code)


modelsData = getModelUrlData()

for modelUrl in modelsData:
    downModel(modelUrl)
